package com.admin.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.admin.dao.AdminOtherDao;
import com.admin.dao.AdminTeachDao;
import com.common.model.AdminUser;
import com.common.model.Course;
import com.common.model.Record;
import com.common.util.Md5Encrypt;
import com.teacher.model.TeachCourse;

@Service
public class AdminTeachService {
	@Autowired
	@Qualifier("adminTeachDao")
	AdminTeachDao dao;
	
	public ArrayList<Course> getAllCourse(int page){
		if(page<1){
			page=1;
		}
		int start=(page-1)*10;
		return dao.getAllCourse(start);
	}
	public int getAllCourseCount(){
		return dao.getAllCourseCount();
	}
	public  Course getCourseByCourseId(int courseId){
		return dao.getCourseByCourseId(courseId);
	}
	public ArrayList<Course> getAllCourseNoPage(){
		return dao.getAllCourseNoPage();
	}
	public int updateCourse(Course course){
		return dao.updateCourse(course);
	}
	public  ArrayList<Course> getAllCourseByCourseNo(String no){
		return dao.getAllCourseByCourseNo(no);
	}
	/**
	 * 修改课程计划
	 * @return -1无此课程
	 */
	public int updateCoursePlan(int courseId ,MultipartFile planFile){
		Course c=dao.getCourseByCourseId(courseId);
		if(c==null){
			return -1;
		}
		HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		ServletContext context=request.getSession().getServletContext();
		String path = request.getContextPath();
		String rootPath=context.getRealPath("/");
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		//清除文件
		int basePathLength=basePath.length();
		String planPath=c.getTeachPlan();
		if(planPath!=null&&!planPath.isEmpty()){
			planPath=context.getRealPath(planPath.substring(basePathLength));
			File file=new File(planPath);
			if(file.exists()){
				file.delete();
			}
		}
		//保存文件
		String fileName="";
		String filePath="";
		String fileExt="";
		String url="";
		int index=planFile.getOriginalFilename().lastIndexOf(".");
		if(index>-1){
			fileExt=planFile.getOriginalFilename().substring(index);
		}
		SimpleDateFormat sim=new SimpleDateFormat("YYYYMMdd");
		fileName=sim.format(new Date())+RandomUtils.nextInt(10000);
		filePath="teachPlan\\";
		url=basePath+filePath+fileName+fileExt;
		url=url.replaceAll("\\\\", "/");
		Course course=new Course();
		course.setId(courseId);
		course.setTeachPlan(url);
		File dir=new File(rootPath+filePath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		try {
			planFile.transferTo(new File(rootPath+filePath+fileName+fileExt));
		} catch (IllegalStateException e) {
			System.out.println(e.getLocalizedMessage());
		} catch (IOException e) {
			System.out.println(e.getLocalizedMessage());
		}
		dao.updateCoursePlan(course);
		return 0;
	}
	public int insertCourse(Course course){
		return dao.insertCourse(course);
	}
	public int deleteCourse(int courseId){
		Course c=dao.getCourseByCourseId(courseId);
		if(c==null){
			return 0;
		}
		HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		ServletContext context=request.getSession().getServletContext();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		//清除文件
		int basePathLength=basePath.length();
		String planPath=c.getTeachPlan();
		if(planPath!=null&&!planPath.isEmpty()){
			planPath=context.getRealPath(planPath.substring(basePathLength));
			File file=new File(planPath);
			if(file.exists()){
				file.delete();
			}
		}
		return dao.deleteCourse(courseId);
	}
	
	public ArrayList<TeachCourse> getTeachCourseListByClgId(int clgId,int page){
		if(page<1){
			page=1;
		}
		int start=(page-1)*10;
		return dao.getTeachCourseListByClgId(clgId, start);
	}
	public int getTeachCourseListCountByClgId(int clgId){
		return dao.getTeachCourseListCountByClgId(clgId);
	}
	public TeachCourse getTeachCourseByTcId(int tcId){
		return dao.getTeachCourseByTcId(tcId);
	}
	public int insertTeachCourse(TeachCourse tc){
		return dao.insertTeachCourse(tc);
	}
	public int deleteTeachCourse(int tcId){
		return dao.deleteTeachCourse(tcId);
	}
}
